<?php

class Utilerias extends MY_Controller{
	private $status = TRUE;
	private $token = '';
	
	function __construct(){
		parent::__construct();
		$this->load->database();
		Sys::loadLanguage($this->params['lang'],'panel');
	}
	
	public function _remap($method, $params){
		$res = $this->checkAccessToken();
		
		$this->load->model('user_model');
		$info = $this->user_model->getInfoUser($this->params);
		$this->params['usr_info'] = $info['user'];
		
		if($res && $this->params['usr_info']['status']==1){
			
			switch ($method) {
				case 'index':
						header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
				break;
				case 'admin':
						$this->admin($method,$params);
				break;
				case 'add':
						$this->add($method,$params);
				break;
				case 'save':
						$this->save();
				break;
				case 'edit':
						$this->add($method,$params);
				break;
				case 'update':
						$this->update($method,$params);
				break;
				case 'del':
						$this->delete($method,$params);
				break;
				default:
					show_404();
				break;
			}
			
		}
		else if(isset($_COOKIE["p_goto"])){ //redireccionar a alguna url despues de logearse
			$url = $_COOKIE["p_goto"];
			setcookie('p_goto', '', time()-1200, '/');
			header("Location: ".$url);
		}else{
	    	header('Location:'.Sys::$url_base.$this->params['lang_url'].'');
		}
		
	}
	
	public function admin($method='', $params=''){
		$this->params['lang_txt_seccion'] = lang('txt_seccion_utileria_admin');
		
		//CSS y JS Base
		$this->carabiner->css(array(
		    array('dev/style.css', 'screen', 'style.css'),
		    array('dev/panel/style.css', 'screen', 'panel-style.css'),
		    array('dev/panel/films/style_admin_films.css', 'screen', 'style_admin_films.css')
		));
		$this->carabiner->js(array(
			array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
			array('dev/libs/tipsy.js', 'screen', 'tip.js'),
			array('dev/panel/general.js', 'screen', 'panel-general.js')
		));

		$this->load->model('utilerias_model');		
		$res_util = $this->utilerias_model->get($this->params);
		
		if(is_array($res_util['utils'])){
			$this->params['list'] = $res_util;
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menu7']		= 'mnu-selected';
		$this->params['selected_submenu11']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		$this->load->view('interface/header', $this->params);
		$this->load->view('interface/panel/content', $this->params);
		$this->load->view('interface/panel/utilerias/admin', $this->params);
		$this->load->view('interface/panel/aside', $this->params);
		$this->load->view('interface/footer', $this->params);
		
		
	}
	
	
	public function add($method = '', $params=''){
		
		//CSS y JS Base
		$this->carabiner->css(array(
		    array('dev/style.css', 'screen', 'style.css'),
		    array('dev/panel/style.css', 'screen', 'panel-style.css'),
		    array('dev/panel/style_form.css', 'screen', 'style_form.css'),
		));
		$this->carabiner->js(array(
			array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
			array('dev/libs/tipsy.js', 'screen', 'tip.js'),
			array('dev/panel/general.js', 'screen', 'panel-general.js')
			
		));
		
		$show_error = FALSE;
		
		if($method === 'add' || $method === 'save'){
			
			$this->params['lang_txt_seccion'] = lang('txt_seccion_utileria_add');
			
			$this->params['title'] 		= '';
			$this->params['content']	= '';
			$this->params['status']		= '1';
			
			// SI EXISTE ALGUN MENSAJE
			if (isset($params['msg_action'])){
				$this->params['class_msg']		= 'msg-ok';
				$this->params['msg_action']	= $params['msg_action'];
			}
	
			// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
			$this->params['selected_menu7']		= 'mnu-selected';
			$this->params['selected_submenu12']	= 'submnu-selected';

			// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
			$this->params['controller'] 	= 'utilerias/';
			$this->params['method'] 		= 'save/';
			$this->params['id']				= '';
			
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> ''
			);
			
		}
		else if($method === 'edit' || $method === 'update'){
			
			// var_dump($params);
			$this->params['id_util'] = $params[0];
			// var_dump($this->params);
			$response_errors = $this->validate('delete');
			
			if($this->status){
					
				$this->load->model('utilerias_model');
				$info_util = $this->utilerias_model->get_one($this->params);
				$info_util = $info_util['utils'];
				
				
				if(is_array($info_util)){
					
					$this->params['lang_txt_seccion'] = lang('txt_seccion_utileria_edit');
					
					$this->params['title'] 		= $info_util[0]['title'];
					$this->params['content']	= $info_util[0]['content'];
					$this->params['status']		= $info_util[0]['status'];
					
					// SI EXISTE ALGUN MENSAJE
					if (isset($params['msg_action'])){
						$this->params['class_msg']		= 'msg-ok';
						$this->params['msg_action']	= $params['msg_action'];
					}
		
					// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
					$this->params['selected_menu6']		= 'mnu-selected';
					$this->params['selected_submenu11']	= 'submnu-selected';
		
					// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
					$this->params['controller'] 	= 'utilerias/';
					$this->params['method'] 		= 'update/';
					$this->params['id']				= $this->params['id_util'];
					
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
				}
				else{
					$show_error = TRUE;
					$this->params['msg'] = lang('txt_utileria_update_error');
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> $this->params['msg']
					);
				}
			}
			else{
				$show_error = TRUE;
				$this->params['msg'] = implode(',', $response_errors);
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> $this->params['msg']
				);
			}			
		}

		if($show_error){
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
		else{
			$this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
			$this->load->view('interface/header',$this->params);
			$this->load->view('interface/panel/content',$this->params);
			$this->load->view('interface/panel/utilerias/form',$this->params);
			$this->load->view('interface/panel/aside',$this->params);
			$this->load->view('interface/footer',$this->params);
		}
		
	}

	public function save(){
		$this->params['status'] = (isset($this->params['status'])) ? '1' : '0';
		
		$response_errors = $this->validate('add');
		if($this->status == TRUE){
			$this->load->model('utilerias_model');
			$util = $this->utilerias_model->add($this->params);
			
			if(is_array($util)){
				$this->add('save',array('msg_action'=>lang('txt_utileria_save_ok')));
			}
				
		}else{
			//CSS y JS Base
			$this->carabiner->css(array(
			    array('dev/style.css', 'screen', 'style.css'),
			    array('dev/panel/style.css', 'screen', 'panel-style.css')
			));
			$this->carabiner->js(array(
				array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
				array('dev/panel/general.js', 'screen', 'panel-general.js')
			));
			
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	public function update($method='', $params=''){
		
		$this->params['id_util'] = $params[0];
		$this->params['status'] = (isset($this->params['status'])) ? '1' : '0';
		
		$response_errors = $this->validate('update');
		if($this->status == TRUE){
			
			$this->load->model('utilerias_model');
			$util = $this->utilerias_model->update($this->params);
			
			if(is_array($util)){
				$this->add('update',array($this->params['id_util'], 'msg_action'=>lang('txt_utileria_update_ok')));
			}

		}else{
			//CSS y JS Base
			$this->carabiner->css(array(
			    array('dev/style.css', 'screen', 'style.css'),
			    array('dev/panel/style.css', 'screen', 'panel-style.css')
			));
			$this->carabiner->js(array(
				array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
				array('dev/panel/general.js', 'screen', 'panel-general.js')
			));
			
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	public function delete($method='', $params=''){
		$this->params['id_util'] = $params[0];
		
		$response_errors = $this->validate('delete');
		if($this->status == TRUE){
			
			$this->load->model('utilerias_model');
			$film = $this->utilerias_model->delete($this->params);
			
			header('Location: '.Sys::$url_base.'panel/utilerias/admin');
		}else{
			//CSS y JS Base
			$this->carabiner->css(array(
			    array('dev/style.css', 'screen', 'style.css'),
			    array('dev/panel/style.css', 'screen', 'panel-style.css')
			));
			$this->carabiner->js(array(
				array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
				array('dev/panel/general.js', 'screen', 'panel-general.js')
			));
			
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    protected function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function validate_add(){
		
		$validate = new MY_Form_validation();
		$validate->setArrayValidate($this->params);

		$config	= array(
					array(
						'field' => 'title',
						'label'	=> 'title',
						'rules'	=> 'required|max_length[130]'
					),
					array(
						'field' => 'content',
						'label'	=> 'content',
						'rules'	=> 'required'
					),
					array(
						'field' => 'enabled',
						'label'	=> 'enabled',
						'rules'	=> 'expression:/^(0|1)$/'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	private function validate_update(){
		
		$validate = new MY_Form_validation();
		$validate->setArrayValidate($this->params);

		$config	= array(
					array(
						'field' => 'title',
						'label'	=> 'title',
						'rules'	=> 'required|max_length[130]'
					),
					array(
						'field' => 'content',
						'label'	=> 'content',
						'rules'	=> 'required'
					),
					array(
						'field' => 'enabled',
						'label'	=> 'enabled',
						'rules'	=> 'expression:/^(0|1)$/'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	private function validate_delete(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate($this->params);
		
		$config	= array(
					array(
						'field' => 'id_util',
						'label'	=> 'id_util',
						'rules'	=> 'required|is_natural_no_zero'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}

}